package br.com.upis.sispat.util;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * The Class HibernateUtil.
 *
 * 
 * Classe utilitária do Hibernate.
 */
public class HibernateUtil {

	/**
	 * Not Instantiates a new hibernate util.
	 */
	private HibernateUtil() {

	}

	/** The session factory. */
	private static SessionFactory sessionFactory;
	
	/** The service registry. */
	private static ServiceRegistry serviceRegistry;
	
	/** The Constant PACKAGE_ENTITY. */
	public static final String PACKAGE_ENTITY = "br.com.upis.sispat.entity.";

	/**
	 * Configure session factory.
	 *
	 * @return the session factory
	 * @throws HibernateException the hibernate exception
	 */
	private static SessionFactory configureSessionFactory()
			throws HibernateException {
		Configuration configuration = new Configuration();
		configuration.configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(
				configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		return sessionFactory;
	}

	/**
	 * Gets the session factory.
	 *
	 * @return the session factory
	 */
	public static SessionFactory getSessionFactory() {
		return configureSessionFactory();
	}

	/**
	 * Gets the entity manager.
	 *
	 * @return the entity manager
	 */
	public static EntityManager getEntityManager() {

		return Persistence.createEntityManagerFactory("HibernateJPAPU")
				.createEntityManager();
	}
	
	
}
